#include "stdafx.h"
#include <QCmdLine.h>
#include <qapplication.h>

StringVector QCmdLine::mArgs;
QCmdLine* QCmdLine::g_Instance = NULL;

QCmdLine::QCmdLine( int argc, char ** argv )
{
	if( argc != 0 )
	{
		for( int i=0; i<argc; i++ )
		{
			mArgs.push_back( argv[i] );
		}
	}
	else{
#ifdef QTOOLS_GUI
		mArgs = qApp->arguments();
#endif
	}
	process( mArgs );
}


QCmdLine::~QCmdLine(void){}

StringVector QCmdLine::arguments(){ return mArgs; }
void QCmdLine::setArguments( const StringVector& vec ){ mArgs = vec; }
void QCmdLine::setArguments( unsigned short count, char ** args ){ mArgs = transform( count, args ); }

void QCmdLine::processFile( const std::string &str ){}

QProcConfig& QCmdLine::process( const StringVector& vec )
{
	StringVector args(arguments());
	QProcConfig ret( QCMD_SUCCESS, QPROC_NONE );
	ret.errors = QCMD_SUCCESS;
	try
	{
		for( auto i=args.begin(); i!=args.end(); i++ )
		{
			if( *i == "nogui"){ ret.switches |= QPROC_NOGUI; }
			else if( *i == "verbose" ){ ret.switches |= QPROC_VERBOSE; }
			else if( *i == "quiet" ){ ret.switches |= QPROC_QUIET; }
			else
				// two word commands
			{
				std::string w;
				std::stringstream ss(*i);
				ss >> w;
				if( w == "exec" ){
					ret.switches |= QPROC_EXECFILE;
					String full;
					while( ss >> w ) full += w +" ";
					processFile( full );
					ret.errors = QCMD_QUIT_OK;
					break;
				}
			}
		}
	}
	catch( ... )
	{
		ret.errors = QCMD_ERROR;
	}
	mConfig = ret;
	return mConfig;
}